/*
 * Email Notify
 * Copyright: (C) 2008 by Antonio Nastasi
 * Email: sifcenter (at) gmail.it
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "configdialog.h"

#include <KPageDialog>
#include <KDebug>

ConfigDialog::ConfigDialog( KConfigGroup& config ) :
    widAccount_(0),
    widAppearance_(0),
    widNotification_(0)
{
  // Set the interface
  setFaceType( KPageDialog::List );
  setCaption( "EmailNotify Configure" );
  setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
  connect( this, SIGNAL( buttonClicked( KDialog::ButtonCode ) ),
           this,   SLOT( slotButtonsClicked( KDialog::ButtonCode ) ) );

  // grep configuration file
  // TODO from pointer by plasma_emailnotify.h
/*  config_       = new KConfig( "emailnotifyrc" );
  generalGroup_ = new KConfigGroup( config_, "General" );
  Q_ASSERT( ( config_ != 0 ) && ( generalGroup_ != 0 ) );*/
  generalGroup_ = config;

  KPageWidgetItem *page = 0 ;

  // set the account page
  widAccount_ = new EditAccount( generalGroup_, this );
  page = new KPageWidgetItem( widAccount_, "Account" );
  page->setHeader( "Account Page" );
  page->setIcon( KIcon( "user-identity" ) );
  addPage( page );

  // set the appearance page
  widAppearance_ = new EditAppearance( generalGroup_, this );
  page = new KPageWidgetItem( widAppearance_, "Appearance" );
  page->setHeader( "Appearance Page" );
  page->setIcon( KIcon( "games-config-background" ) );
  addPage( page );

  //set the notification page
  widNotification_ = new EditNotification( generalGroup_, this );
  page = new KPageWidgetItem( widNotification_, "Notification" );
  page->setHeader( "Notification Page" );
  page->setIcon( KIcon( "preferences-desktop-notification" ) );
  addPage( page );
}



ConfigDialog::~ConfigDialog()
{
  delete widAccount_;
  delete widNotification_;
  delete widAppearance_;
  kDebug() << "DESTROYED";
}



void ConfigDialog::loadConfig()
{
    widAccount_     ->loadConfig();
    widNotification_->loadConfig();
    widAppearance_  ->loadConfig();
}



void ConfigDialog::saveConfig()
{
  // Check if the config is not null
  widAccount_     ->saveConfig();
  widNotification_->saveConfig();
  widAppearance_  ->saveConfig();
  generalGroup_.config()->sync();

  emit configUpdated();
}



void ConfigDialog::slotButtonsClicked( KDialog::ButtonCode button )
{
  switch( button )
  {
     //FIXME media->stop();
    case KDialog::Ok:
      saveConfig();
    case KDialog::Cancel:
      hide();
      break;
    case KDialog::Apply:
      saveConfig();
      break;
    default:
      break;
  }
}



void ConfigDialog::show()
{
  loadConfig();
  KPageDialog::show();
}
